/* Copyright (C) 2005-2013, Unigine Corp. All rights reserved.
 *
 * File:    fragment_lut_light.h
 * Desc:    Mesh lut light shader
 * Version: 1.01
 * Author:  Alexander Zapryagaev <frustum@unigine.com>
 *
 * This file is part of the Unigine engine (http://unigine.com/).
 *
 * Your use and or redistribution of this software in source and / or
 * binary form, with or without modification, is subject to: (i) your
 * ongoing acceptance of and compliance with the terms and conditions of
 * the Unigine License Agreement; and (ii) your inclusion of this notice
 * in any version of this software that you use or redistribute.
 * A copy of the Unigine License Agreement is available by contacting
 * Unigine Corp. at http://unigine.com/
 */

/******************************************************************************\
*
* OpenGL
*
\******************************************************************************/

#ifdef OPENGL
	
	#ifndef FRAGMENT_LUT_LIGHT
		#define FRAGMENT_LUT_LIGHT
		
		uniform sampler2D s_texture_0;
		uniform sampler2D s_texture_1;
		uniform sampler2D s_texture_2;
		uniform sampler2D s_texture_3;
		uniform sampler2D s_texture_4;
		
		uniform half4 detail_transform;
		
	#else
		
		#include <core/shaders/default/mesh/fragment_lut_sample.h>
		
		half3 camera_direction = normalize(s_texcoord_2.xyz);
		
		#ifdef WORLD
			half3 light_direction = s_texcoord_1.xyz;
			half diffuse_shading = dot(light_direction,normal) * 0.5f + 0.5f;
			half specular_shading = dot(reflect(-light_direction,normal),camera_direction);
			half3 color = diffuse.xyz * texture(s_texture_2,half2(diffuse_shading,specular_shading)).xyz * (4.0f * s_material_shading.x);
		#else
			half4 light_direction = s_texcoord_1;
			half light_direction_length2 = dot(light_direction.xyz,light_direction.xyz);
			half light_direction_ilength = rsqrt(light_direction_length2);
			half light_attenuation = getAttenuation(light_direction.w - light_direction_length2);
			half diffuse_shading = dot(light_direction.xyz,normal) * light_direction_ilength * 0.5f + 0.5f;
			half specular_shading = dot(reflect(-light_direction.xyz,normal),camera_direction) * light_direction_ilength;
			half3 color = diffuse.xyz * texture(s_texture_2,half2(diffuse_shading,specular_shading)).xyz * (4.0f * s_material_shading.x * light_attenuation);
		#endif
		
	#endif
	
/******************************************************************************\
*
* Direct3D11
*
\******************************************************************************/

#elif DIRECT3D11
	
	#ifndef FRAGMENT_LUT_LIGHT
		#define FRAGMENT_LUT_LIGHT
		
		Texture2D s_texture_0 : register(t0);
		Texture2D s_texture_1 : register(t1);
		Texture2D s_texture_2 : register(t2);
		Texture2D s_texture_3 : register(t3);
		Texture2D s_texture_4 : register(t4);
		
		cbuffer shader_parameters {
			#ifdef OVERLAY_0 || OVERLAY_1 || MULTIPLY_0 || MULTIPLY_1
				float4 detail_transform;
			#endif
		};
		
	#else
		
		#include <core/shaders/default/mesh/fragment_lut_sample.h>
		
		half3 camera_direction = normalize(IN.texcoord_2.xyz);
		
		#ifdef WORLD
			half3 light_direction = IN.texcoord_1.xyz;
			half diffuse_shading = dot(light_direction,normal) * 0.5f + 0.5f;
			half specular_shading = dot(reflect(-light_direction,normal),camera_direction);
			half3 color = diffuse.xyz * s_texture_2.Sample(s_sampler_2,half2(diffuse_shading,specular_shading)).xyz * (4.0f * s_material_shading.x);
		#else
			half4 light_direction = IN.texcoord_1;
			half light_direction_length2 = dot(light_direction.xyz,light_direction.xyz);
			half light_direction_ilength = rsqrt(light_direction_length2);
			half light_attenuation = getAttenuation(light_direction.w - light_direction_length2);
			half diffuse_shading = dot(light_direction.xyz,normal) * light_direction_ilength * 0.5f + 0.5f;
			half specular_shading = dot(reflect(-light_direction.xyz,normal),camera_direction) * light_direction_ilength;
			half3 color = diffuse.xyz * s_texture_2.Sample(s_sampler_2,half2(diffuse_shading,specular_shading)).xyz * (4.0f * s_material_shading.x * light_attenuation);
		#endif
		
	#endif
	
/******************************************************************************\
*
* Direct3D9
*
\******************************************************************************/

#elif DIRECT3D9
	
	#ifndef FRAGMENT_LUT_LIGHT
		#define FRAGMENT_LUT_LIGHT
		
		half4 detail_transform;
		
	#else
		
		#include <core/shaders/default/mesh/fragment_lut_sample.h>
		
		half3 camera_direction = normalize(IN.texcoord_2.xyz);
		
		#ifdef WORLD
			half3 light_direction = IN.texcoord_1.xyz;
			half diffuse_shading = dot(light_direction,normal) * 0.5f + 0.5f;
			half specular_shading = dot(reflect(-light_direction,normal),camera_direction);
			half3 color = diffuse.xyz * tex2D(s_texture_2,half2(diffuse_shading,specular_shading)).xyz * (4.0f * s_material_shading.x);
		#else
			half4 light_direction = IN.texcoord_1;
			half light_direction_length2 = dot(light_direction.xyz,light_direction.xyz);
			half light_direction_ilength = rsqrt(light_direction_length2);
			half light_attenuation = getAttenuation(light_direction.w - light_direction_length2);
			half diffuse_shading = dot(light_direction.xyz,normal) * light_direction_ilength * 0.5f + 0.5f;
			half specular_shading = dot(reflect(-light_direction.xyz,normal),camera_direction) * light_direction_ilength;
			half3 color = diffuse.xyz * tex2D(s_texture_2,half2(diffuse_shading,specular_shading)).xyz * (4.0f * s_material_shading.x * light_attenuation);
		#endif
		
	#endif
	
/******************************************************************************\
*
* PlayStation3
*
\******************************************************************************/

#elif PLAYSTATION3
	
	#ifndef FRAGMENT_LUT_LIGHT
		#define FRAGMENT_LUT_LIGHT
		
		uniform sampler2D s_texture_0 : TEXUNIT0;
		uniform sampler2D s_texture_1 : TEXUNIT1;
		uniform sampler2D s_texture_2 : TEXUNIT2;
		uniform sampler2D s_texture_3 : TEXUNIT3;
		uniform sampler2D s_texture_4 : TEXUNIT4;
		
		uniform half4 detail_transform;
		
	#else
		
		#include <core/shaders/default/mesh/fragment_lut_sample.h>
		
		half3 camera_direction = normalize(IN.texcoord_2.xyz);
		
		#ifdef WORLD
			half3 light_direction = IN.texcoord_1.xyz;
			half diffuse_shading = dot(light_direction,normal) * 0.5f + 0.5f;
			half specular_shading = dot(reflect(-light_direction,normal),camera_direction);
			half3 color = diffuse.xyz * h3tex2D(s_texture_2,half2(diffuse_shading,specular_shading)) * (4.0f * s_material_shading.x);
		#else
			half4 light_direction = IN.texcoord_1;
			half light_direction_length2 = dot(light_direction.xyz,light_direction.xyz);
			half light_direction_ilength = rsqrt(light_direction_length2);
			half light_attenuation = getAttenuation(light_direction.w - light_direction_length2);
			half diffuse_shading = dot(light_direction.xyz,normal) * light_direction_ilength * 0.5f + 0.5f;
			half specular_shading = dot(reflect(-light_direction.xyz,normal),camera_direction) * light_direction_ilength;
			half3 color = diffuse.xyz * h3tex2D(s_texture_2,half2(diffuse_shading,specular_shading)) * (4.0f * s_material_shading.x * light_attenuation);
		#endif
		
	#endif
	
#endif
